package com.guoqiang.class_two.one;

import java.util.Arrays;
import java.util.Random;

/**
 * 定义一个长度为[16][16]的整型二维数组并输入或指定所有位置的元素值，分别实现二维数组中所有行和所有列中所有元素的累加和并打印。
 * 再分别实现二维数组中左上角到右下角和右上角到左下角所有元素的累加和并打印。
 */
public class ScratchesOne {
    public static void main(String[] args) {

        System.out.println("----------------------定义数组、赋值、打印---------------------");
        int[][] arr = new int[16][16];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] = random.nextInt(10);
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(Arrays.toString(arr[i]));
        }

        System.out.println("\n");
        System.out.println("----------------------求和---------------------");
        int sum = 0;
        int sumFromRightTop = 0;
        int sumFromLeftTop = 0;
        for (int i = 0, j = 15; i < arr.length; i++,j--) {
            sum += Arrays.stream(arr[i]).sum();
            sumFromRightTop += arr[i][j];
            sumFromLeftTop += arr[i][i];
        }
        System.out.println("所有行列和为：" + sum);
        System.out.println("左上角到右下角：" + sumFromLeftTop);
        System.out.println("右上角到做下脚：" + sumFromRightTop);


    }


}
